package com.example.tyz1201.myaccount.Activity.Activity.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tyz1201.myaccount.Activity.Activity.Model.AssumeModel;
import com.example.tyz1201.myaccount.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private float monthMoneyDifference, todayDifference;
    private long createTime, systemTime;
    private int choosedAssumeTypeId;
    private String assumeData, createTimeStr, systemTimeStr, str;
    private Date createDate, systemDate;
    private Calendar createCalendar, systemCalendar;
    RelativeLayout rl_yesterday_diff, rl_today_diff;
    LinearLayout ll_month_income, ll_month_outcome, ll_month_diff;
    Map<String, String> map = new HashMap<>();
    AssumeModel assumeModel;
    Button bt_read, bt_write;
    TextView tv_today_diff, tv_yesterday_diff, tv_month_income, tv_month_outcome, tv_month_diff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt_write = (Button) findViewById(R.id.bt_write);
        bt_read = (Button) findViewById(R.id.bt_read);

        bt_write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, WriteActivity.class);
                startActivity(intent);
            }
        });

        bt_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ReadActivity.class);
                startActivity(intent);
            }
        });

        tv_today_diff = (TextView) findViewById(R.id.tv_today_diff);
        tv_yesterday_diff = (TextView) findViewById(R.id.tv_yesterday_diff);

        tv_month_income = (TextView) findViewById(R.id.tv_month_income);     //找到需要显示的组件
        tv_month_outcome = (TextView) findViewById(R.id.tv_month_outcome);
        tv_month_diff = (TextView) findViewById(R.id.tv_month_diff);

        ll_month_income = (LinearLayout) findViewById(R.id.ll_month_income);
        ll_month_income.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MonthIncomeActivity.class);
                startActivity(intent);
            }
        });

        ll_month_outcome = (LinearLayout) findViewById(R.id.ll_month_outcome);
        ll_month_outcome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MonthOutcomeActivity.class);
                startActivity(intent);
            }
        });

        ll_month_diff = (LinearLayout) findViewById(R.id.ll_month_diff);
        ll_month_diff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MonthDifferenceActivity.class);
                startActivity(intent);
            }
        });

        rl_today_diff = (RelativeLayout) findViewById(R.id.rl_today_diff);
        rl_yesterday_diff = (RelativeLayout) findViewById(R.id.rl_yesterday_diff);

    }

    @Override
    protected void onResume() {
        SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
        map = (Map<String, String>) sharedPreferences.getAll();

        Set<String> keySet = map.keySet();
        float dayMoney1 = 0, dayMoney2 = 0, monthOutcome1 = 0, monthIncome1 = 0;

        if (!(map.size() == 0)) {


            for (String key : keySet) {
                assumeData = map.get(key);
                Log.d("err", assumeData);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                createTime = assumeModel.getCreateTime(); //long 类型日期
                systemTime = System.currentTimeMillis();

                createDate = new Date(createTime); // Date 类型日期
                systemDate = new Date(systemTime);

                SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd");
                createTimeStr = sf.format(createDate); //createTimeStr字符串类型日期
                systemTimeStr = sf.format(systemDate);

                createCalendar = Calendar.getInstance();
                systemCalendar = Calendar.getInstance();
                createCalendar.setTime(createDate); // Calendar 类型日期
                systemCalendar.setTime(systemDate);
/**
 *  今日差额与昨日差额的计算
 */
                if (systemTimeStr.equals(createTimeStr)) {
                    choosedAssumeTypeId = assumeModel.getChoosedAssumeTypeId();
                    if (choosedAssumeTypeId == -1) {                                         //今日差额
                        dayMoney1 = dayMoney1 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                    } else if (choosedAssumeTypeId == 1) {

                        dayMoney1 = dayMoney1 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                    }
                    tv_today_diff.setText("" + dayMoney1);
                } else {
                    Calendar calendar = systemCalendar;
                    calendar.add(Calendar.DAY_OF_YEAR, -1);  //昨日差额 日期格式的转换
                    Date date = calendar.getTime();
                    String str = sf.format(date);


                    if (createTimeStr.equals(str)) {
                        if (choosedAssumeTypeId == -1) {
                            dayMoney2 = dayMoney2 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                        } else if (choosedAssumeTypeId == 1) {
                            dayMoney2 = dayMoney2 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                        }
                        tv_yesterday_diff.setText("" + dayMoney2);

                    }

                }

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
                createTimeStr = simpleDateFormat.format(createDate);
                systemTimeStr = simpleDateFormat.format(systemDate);

                if (createTimeStr.equals(systemTimeStr)) {

                    if (choosedAssumeTypeId == -1) {
                        monthOutcome1 = monthOutcome1 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                        tv_month_outcome.setText("" + monthOutcome1);
                    } else if (choosedAssumeTypeId == 1) {
                        monthIncome1 = monthIncome1 + choosedAssumeTypeId * assumeModel.getTodoMoney();
                        tv_month_income.setText("" + monthIncome1);
                    }
                    monthMoneyDifference = monthIncome1 + monthOutcome1;
                    tv_month_diff.setText("" + monthMoneyDifference);
                }
            }
        } else {
            tv_today_diff.setText("0.0");
            tv_yesterday_diff.setText("0.0");
            tv_month_diff.setText("0.0");
            tv_month_income.setText("0.0");
            tv_month_outcome.setText("0.0");
        }

        todayDifference = Float.parseFloat(tv_today_diff.getText().toString());
        if (todayDifference >= 35) {
            str = "Come on , you are enough !";
        } else {
            str = "Not bad , you should keep stick at it !";
        }

        rl_today_diff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("tips : 你该再接再厉还是迷途知返 ？")
                        .setMessage(str)
                        .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                toDoSomethingGood();
                            }
                        })
                        .setNegativeButton("哦", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                toDoSomethingBad();
                            }
                        })
                        .create();
                alertDialog.show();
            }
        });

        rl_yesterday_diff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("tips : 你该再接再厉还是迷途知返 ？")
                        .setMessage(str)
                        .setPositiveButton("恩恩", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                toDoSomethingGood();
                            }
                        })
                        .setNegativeButton("哦", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                toDoSomethingBad();
                            }
                        })
                        .create();
                alertDialog.show();
            }
        });

        super.onResume();
    }

    private void toDoSomethingBad() {
        AlertDialog alertDialog1 = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Who are you ?")
                .setMessage("I don't know ?")
                .setPositiveButton("Gun_Du_Zi", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .create();
        alertDialog1.show();
    }

    private void toDoSomethingGood() {
        AlertDialog alertDialog1 = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Words of thanks")
                .setMessage("Thanks , glad to hear that !")
                .setPositiveButton("Byebye", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .create();
        alertDialog1.show();
    }

}
